package com.syq.interceptors;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.syq.constants.ResultCode;
import com.syq.dto.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Sun Yueqi
 * @version 1.00
 * @time 2022/5/29 22:40
 */
@Controller
public class MyLoginInterceptors implements HandlerInterceptor {
    private ObjectMapper objectMapper=new ObjectMapper();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user=request.getSession().getAttribute("user");
        if (user==null){
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(objectMapper.writeValueAsString(R.setResult(ResultCode.NOT_LOGIN,"未登录",null)));
        }
        return user!=null;
    }
}
